/*
 * 线上环境webpack配置
 * @author li.xiaomei
 * @date 2016-12-19
 */
var webpack = require('webpack')
var merge = require('webpack-merge')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var baseConfig = require('./base.config.js')
var commonMethod = require('../method.js')
var global_config = global.think_config
var webpack_build = global_config.webpack.build
var ip = require('../ip')()

var webConfig = {
  module: {
    loaders: commonMethod.styleLoaders({
      sourceMap: webpack_build.sourceMap,
      extract: webpack_build.extract
    })
  },

  devtool: webpack_build.sourceMap ? '#source-map' : false,

  output: webpack_build.output,

  vue: {
    loaders: commonMethod.cssLoaders({
      sourceMap: webpack_build.sourceMap,
      extract: true
    })
  },

  plugins: [
    // http://vuejs.github.io/vue-loader/workflow/production.html
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
        booleans: false,
        unused: false
      }
    }),
    new webpack.optimize.OccurenceOrderPlugin(),

  ]
}

//是否需要打包
if (webpack_build.productionGzip) {
  var CompressionWebpackPlugin = require('compression-webpack-plugin')

  webConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(' +
        webpack_build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}

//将
if(commonMethod.findArgv(global.build_beta)) {
  webConfig.output.publicPath = 'http://'+ip.extra+':'+global_config.port;
}else {
  //将css独立成文件
  webConfig.plugins.push(
    new ExtractTextPlugin(webpack_build.cssname)
  )
}

module.exports = function(){
  return merge(baseConfig('build'), webConfig);
}
